package com.asm.DBloader;

import java.util.ArrayList;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

public class ASM_fDaynPrdDetDS {
	private SQLiteDatabase dB;
	private ASM_DB dbHelper;
	Context context;
	
	
	public ASM_fDaynPrdDetDS(Context context) {
		this.context = context;
		dbHelper  = new ASM_DB(context);
		
	}
	
	public void open() throws SQLException
	{
		dB = dbHelper.getWritableDatabase();
	}
	public int Create(ASM_fDaynPrdDet det) 
	{
		
		int count =0;
		
		if(dB == null){
			open();
		}else if(!dB.isOpen()){
			open();
		}

		ContentValues values = new ContentValues();
		
		values.put(ASM_DB.asmFnonDet_RefNo, det.getAsmFnonDet_RefNo());
		values.put(ASM_DB.asmFnonDet_TxnDate, det.getAsmFnonDet_TxnDate());
		values.put(ASM_DB.asmFnonDet_DebCode, det.getAsmFnonDet_DebCode());
		values.put(ASM_DB.asmFnonDet_Reason, det.getAsmFnonDet_Reason());
		values.put(ASM_DB.asmFnonDet_RecordId, det.getAsmFnonDet_RecordId());
		values.put(ASM_DB.asmFnonDet_Longitude, det.getAsmFnonDet_Longitude());
		values.put(ASM_DB.asmFnonDet_Latitude, det.getAsmFnonDet_Latitude());
		values.put(ASM_DB.asmFnonDet_RepCode, det.getAsmFnonDet_RepCode());
		
		values.put(ASM_DB.asmFnonDet_Teratory, det.getAsmFnonDet_Teratory());
		values.put(ASM_DB.asmFnonDet_Syncstate, det.getAsmFnonDet_Syncstate());

		count = (int) dB.insert(ASM_DB.ASM_FDAYNPRDDET, null, values);

		dB.close();
		return count;
		
	}
	
	public ArrayList<String> getfDaynPrdDet(String refNo) 
	{
		if (dB == null) 
		{
			open();
		} else if (!dB.isOpen()) 
		{
			open();
		}
		ArrayList<String> Details = new ArrayList<String>();
		
	
		String selectQuery = "select * from fDaynPrdDet where RefNo = '"+refNo+"'";
		
		Cursor cursor = null;
		cursor = dB.rawQuery(selectQuery, null);
		
		while(cursor.moveToNext())
		{
		
			String RefNo = cursor.getString(1);
			String TxnDate = cursor.getString(2);
			String DebCode = cursor.getString(3);
			String Reason = cursor.getString(4);
			String RecordId = cursor.getString(5);
			String Longitude = cursor.getString(6);
			String Latitude = cursor.getString(7);
			String RepCode = cursor.getString(8);
			Details.add(RefNo);
			Details.add(TxnDate);
			Details.add(DebCode);
			Details.add(Reason);
			Details.add(RecordId);
			Details.add(Longitude);
			Details.add(Latitude);
			Details.add(RepCode);
			
			
			
		}
		
		return Details;
	}
	
	public ArrayList<String> getfDaynPrdDetall() 
	{
		if (dB == null) 
		{
			open();
		} else if (!dB.isOpen()) 
		{
			open();
		}
		ArrayList<String> Details = new ArrayList<String>();
		
	
		String selectQuery = "select Reason from fDaynPrdDet ";
		
		Cursor cursor = null;
		cursor = dB.rawQuery(selectQuery, null);
		
		while(cursor.moveToNext())
		{
		
		
			String Reason = cursor.getString(0);
		
		
			Details.add(Reason);
		
			
			
			
		}
		
		return Details;
	}
	public int Delete(String Refno)
	{
		
		int count =0;
		
		if(dB == null){
			open();
		}else if(!dB.isOpen()){
			open();
		}

		count = (int) dB.delete(ASM_DB.ASM_FDAYNPRDDET, "RefNo = '"+Refno+"'", null);

		dB.close();
		return count;
		
	}
	public int Update(ASM_fDaynPrdDet det) 
	{
		
		int count =0;
		
		if(dB == null)
		{
			open();
		}else if(!dB.isOpen())
		{
			open();
		}

		ContentValues values = new ContentValues();
		
		values.put(ASM_DB.asmFnonDet_RefNo, det.getAsmFnonDet_RefNo());
		values.put(ASM_DB.asmFnonDet_TxnDate, det.getAsmFnonDet_TxnDate());
		values.put(ASM_DB.asmFnonDet_DebCode, det.getAsmFnonDet_DebCode());
		values.put(ASM_DB.asmFnonDet_Reason, det.getAsmFnonDet_Reason());
		values.put(ASM_DB.asmFnonDet_RecordId, det.getAsmFnonDet_RecordId());
		values.put(ASM_DB.asmFnonDet_Longitude, det.getAsmFnonDet_Longitude());
		values.put(ASM_DB.asmFnonDet_Latitude, det.getAsmFnonDet_Latitude());
		values.put(ASM_DB.asmFnonDet_RepCode, det.getAsmFnonDet_RepCode());
	
		count = (int) dB.update(ASM_DB.ASM_FDAYNPRDDET, values, "RefNo = '"+det.getAsmFnonDet_RefNo().toString()+"'", null);

		dB.close();
		return count;
		
	}
}
